<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:head="http://www.w3.org/1999/html">
<head th:include="public/public_css::public_css ('新增退货单')"/>

<body style="background-color: #f2f2f2;">
<div class="layui-fluid larry-wrapper">
    <div class="layui-card">
        <form class="layui-form layui-card-header  layui-card-header-auto" id="formTj">
            <div>
                <div class="layui-inline">
                    <label class="layui-form-label">联系人</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input"  name="lxr" id="lxr"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">SO编号</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input"  name="sobh" id="sobh"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">联系方式</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input"  name="lxrdh" id="lxrdh"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">详细地址</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input"  name="xxdz" id="xxdz"/>
                    </div>
                </div>
                <div class="layui-inline inline-search-btn">
                    <a class="layui-btn layui-btn-normal search_btn" lay-event="searchEvent" data-type="reload">查询</a>
                </div>
                <div class="layui-inline inline-reset-btn">
                    <button type="reset" class="layui-btn layui-btn-normal" >重置</button>
                </div>
                <div class="layui-col-xs24" >
                    <table data-height="300" id="table"></table>
                </div>
                <div class="layui-col-xs24" style="max-height: 300px;overflow: auto">
                    <table data-height="300" id="table1"></table>
                </div>
                <div class="layui-input-block" style="float: right;">
                    <button class="layui-btn" lay-submit lay-filter="add">制作</button>
                </div>
            </div>
        </form>
</div>
</div>
<div style="padding: 0px 15px;" hidden="hidden" id="addfunction">

    <div class="layui-fluid larry-wrapper">
        <div class="layui-card">
            <!--  <div class="layui-row layui-col-space30">-->
            <!--<div class="layui-col-xs24">-->
            <form class="layui-form layui-card-header  layui-card-header-auto" id="myform">
                    <div class="layui-inline">
                        <label class="layui-form-label">快递单号</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" lay-verify="required" name="wldh" id="wldh"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">收发类别</label>
                        <div class="layui-input-inline">
                            <select lay-ignore="lay-ignore" class="js-data-example-ajax" name="asnLxid" id="asnLxid"
                                    style="width: 193px"></select>
                        </div>
                    </div>
                <input type="hidden" id="parentIndex" name="parentIndex"><!--父行的索引-->
                <div class="layui-col-xs24" >
                    <table	 id="table2" class="table text-nowrap"></table>
                </div>
                <div class="layui-input-block" style="float: right;">
                    <button class="layui-btn" lay-submit lay-filter="save">保存</button>
                </div>
            </form>
        </div>
        <div>
        </div>
    </div>
</div>
</body>

<div th:include="public/public_js::public_js"></div>
<div th:include="public/public_btn::public_btn"></div>
<script th:inline="javascript">
    var array = [];
    $(function () {
        //查询库区
        $('.js-data-example-ajax').select2({
            ajax: {
                url: '/zxsdwms/wmsRejected/getOrderTypeCk',
                dataType: 'json',
                type: 'POST',
                contentType: "application/json",
                data: function (params) {
                    var query = {
                        mc: params.term
                    };
                    return JSON.stringify(query);
                },
                processResults: function (data) {
                    var arr = new Array();
                    console.log('data', data);
                    if (data) {
                        for (var i = 0; i < data.length; i++) {
                            arr.push({id: data[i].dictionaryid, text: data[i].value});
                        }
                    }
                    console.log(arr);
                    return {
                        results: arr
                    };
                },
                cache: true
            },
            language: "zh-CN",
            placeholder: '请输入查询字段',
            allowClear: true
        });

    })
    var xx_number = 0;
    layui.use(['myform','zzpUtils','form','laydate','jquery','zzpValidation'],function () {
        var form = layui.form,
            zzpUtils = layui.zzpUtils
            ,laydate = layui.laydate
            ,$ = layui.jquery;
        var $table2;
        var $table = zzpUtils.table({
            elem: '#table',
            isApi: true,
            pagination:true,
            showColumns: false,
            url:"/wmsRejected/soList",
            columns: [{
                field: 'sobh',
                title: 'SO编号'
            }, {
                field: 'ddlx',
                title: '订单类型'
            }, {
                field: 'sozt',
                title: 'SO状态'
            }, {
                field: 'khbh',
                title: '客户编号'
            }, {
                field: 'khmc',
                title: '客户名称'
            }, {
                field: 'sheng',
                title: '省'
            }, {
                field: 'shi',
                title: '市'
            }, {
                field: 'xxdz',
                title: '详细地址'
            }, {
                field: 'lxr',
                title: '联系人'
            }, {
                field: 'lxrdh',
                title: '联系电话'
            }, {
                field: 'fyrq',
                title: '发运日期'
            }],
            detailFormatter: function (index, row) {
                return "这里就没有确定过11";
            },
            onDblClickRow: function (row) {
                var data = $table1.bootstrapTable('getData');
                var returnrows = $.map(data,function (rowdata) {
                    if(rowdata.id==row.id){
                        return rowdata.id;
                    }
                })
                if(returnrows.length>0){
                    alert("已存在不能重复添加");
                }else{
                    var newdata = $.extend(true,[],row); //复制一份数据
                        $table1.bootstrapTable("append",row);
                }
            },
            queryParams: function () {//查询条件
                return zzpUtils.getFormJson($("#formTj"));
            },
            detailFormatter: function (index, row) {
                return "这里就没有确定过11";
            }
        });
        var $table1 = zzpUtils.table({
            elem: '#table1',
            isApi: true,
            pagination:false,
            showColumns: false,
            url: '[{}]',
            columns: [{
                field: 'sobh',
                title: 'SO编号'
            }, {
                field: 'ddlx',
                title: '订单类型'
            }, {
                field: 'sozt',
                title: 'SO状态'
            }, {
                field: 'khbh',
                title: '客户编号'
            }, {
                field: 'khmc',
                title: '客户名称'
            }, {
                field: 'sheng',
                title: '省'
            }, {
                field: 'shi',
                title: '市'
            }, {
                field: 'xxdz',
                title: '详细地址'
            }, {
                field: 'lxr',
                title: '联系人'
            }, {
                field: 'lxrdh',
                title: '联系电话'
            }, {
                field: 'fyrq',
                title: '发运日期'
            }],
            detailFormatter: function (index , row) {
                return "这里就没有确定过11";
            },
        });
        //添加一行
        $("#addbtn").on('click',function () {
            var isRepeat;
            var $rows = $table.bootstrapTable('getSelections');
            console.log('$row', $rows);
            var tabledata = $table1.bootstrapTable('getData');
            if (tabledata.length ===0) {
                $table1.bootstrapTable('append', $rows);
            } else {
                for (var i=0;i<$rows.length;i++){
                    isRepeat = tabledata.every(function (item) {
                        return (item.id !== $rows[i].id)
                    });
                    if (!isRepeat){
                        break;
                    }
                }
                console.log('isRepeat', isRepeat);
                if (isRepeat) {
                    $table1.bootstrapTable('append', $rows);
                } else {
                    layer.msg('已存在不能重复添加');
                }
            }
        });
        // 制作
        form.on("submit(add)",function(data){
            var field = data.field;
            var ids=[];
            //获取table数据
            var TableData = $table1.bootstrapTable('getData');
            if (TableData.length > 0) {
                TableData.forEach(function (item) {
                    var items = item;
                    delete items.index;
                    array.push(items.id);
                });
                layer.open({
                    type: 1,
                    title: "制作",
                    content: $("#addfunction"),
                    area: ['950px', '600px'],
                    shade: [0.4, '#393D49'],
                    success: function (layero, index) {
                            $table2 = zzpUtils.table({
                            elem: '#table2',
                            url: "/wmsRejectedDetailed/list",
                            isApi: true,
                            pagination:true,
                            queryParams :queryParams1,
                            showColumns: false,
                            columns: [{
                                field: 'sobh',
                                title: 'SO编号'
                            }, {
                                field: 'ddlx',
                                title: '订单类型'
                            }, {
                                field: 'sozt',
                                title: 'SO状态'
                            }, {
                                field: 'khbh',
                                title: '客户编号'
                            }, {
                                field: 'khmc',
                                title: '客户名称'
                            }, {
                                field: 'hpbh',
                                title: '货品编号'
                            }, {
                                field: 'hpmc',
                                title: '货品名称'
                            }, {
                                field: 'pc',
                                title: '货品批次'
                            }, {
                                field: 'kthsl',
                                title: '可退货数量'
                            }, {
                                field: 'thsl',
                                title: '退货数量(点击编辑)',
                                formatter: operateFormatterNumber688,
                                editable: {
                                    type: 'text',
                                    clear: false,
                                    validate: function (value) {
                                        if (isNaN(value)) return {newValue: 1, msg: '只允许输入数字'};
                                        else if (value <=0) return {newValue: 1,msg: '不能输入负数或0'};
                                        else if (value <= xx_number) return {newValue: value};
                                        else if (value > xx_number) return {newValue: xx_number};
                                    },
                                }
                            }],
                            onClickRow: function (row, $element, field) {
                                xx_number = row.kthsl;
                            },
                            detailFormatter: function (index , row) {
                                return "这里就没有确定过11";
                            },
                        });

                    }

                })
            } else {
                layer.msg('您还不能保存，退货单为必填项');
            }
            return false;
        })
        function queryParams1(params) {
            var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                ids: array,
                page: 1,
                rows: 10
            };
            return temp;
        };
        function operateFormatterNumber688(value, row, index) {
            return 0;
        }
        form.on("submit(save)",function(data){
            var field = data.field;
            var wldh=$("#wldh").val();
            var asnLxid=$("#asnLxid").val();
            zzpUtils.confirm("是否确认添加",function(index){
                var wardList=[];
                var SaveData = {};
                var thsl=0;
                //获取table数据
                var TableData = $table2.bootstrapTable('getData');
                if (TableData.length > 0) {
                    TableData.forEach(function (item) {
                        var items = item;
                        delete items.index;
                        wardList.push(items);
                        thsl=items.thsl;
                    });
                    SaveData.wldh = wldh;
                    SaveData.asnLxid = asnLxid;
                    SaveData.wmsRejectedDetailedList = wardList;
                    if(thsl>0){
                        zzpUtils.action({
                            url:"/wmsRejected/add",
                            data: JSON.stringify(SaveData),
                            success:function (data) {
                                setTimeout(function(){
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                    top.layer.msg(data.title);
                                },500);
                            },error: function (xhr, textStatus) {
                                console.log(textStatus);
                                console.log(xhr);
                            }
                        })
                    }else{
                        layer.msg('退货数量必须大于0');
                    }
                } else {
                    layer.msg('您还不能保存，信息不可为空');
                }
            })
            return false;
        })
        $(".inline-search-btn").on('click', function () {
            zzpUtils.refreshTable("#table");
        });
    });
</script>
</html>